/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package researchgrants.parts.LoggedData.panels.lists;

import java.awt.event.ActionEvent;
import researchgrants.parts.LoggedData.panels.LoggedDataPanels;
import java.awt.Component;
import java.awt.event.ActionListener;
import java.util.SortedMap;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.event.ChangeEvent;

/**
 *
 * @author DOStudent1
 */
public abstract class LoggedDataPanelsList extends LoggedDataPanels implements LoggedDataListInterface, ActionListener {
    public String getTextByID(Object id)  {
        if (!allMap.containsKey(id)) {
            throw new RuntimeException("Could not find key '" + id + "' in all keys");
        }
        return((String)allMap.get(id));
    }
    
    String selectedId;
    private boolean hasSelectedValue=false;

    @Override
    public Component getViewComponent() {
        PopulateData();
        validateDataToDisplay();
        JLabel lblViewData=new JLabel();
        if (!selectedId.equals("")) {
            lblViewData.setText(getTextByID(selectedId));
        }
        return(lblViewData);
    }

    protected JComboBox fldList;

    final String Empty_Key="";
    SortedMap<String, String> allMap;
    
    String[] allMapKeys;

    @Override
    public Component getEditComponent() {
        PopulateData();
        fldList=new JComboBox(allMap.values().toArray());
        fldList.addActionListener(this);
        updateSelect();
        return(fldList);
    }

    @Override
    public String getEdittedValue() {
        if (fldList.getSelectedIndex()==-1) {
            return("");
        } else {
            return(allMapKeys[fldList.getSelectedIndex()]);
        }
    }

    
    public void setSelected(int selectedId) {
        if (hasDataInitialized()) {
            throw new RuntimeException("Cannot request to select an item,  " +
                    "if it has already have a value");
        }
        this.selectedId=Integer.toString(selectedId);
        hasSelectedValue=true;
        if (fldList!=null) {
            updateSelect();
        }
    }

    public void setSelected(String selectedValue) {
        if (hasDataInitialized()) {
            throw new RuntimeException("Cannot request to select an item,  " +
                    "if it has already have a value");
        }
        this.selectedId=selectedValue;
        hasSelectedValue=true;
        if (fldList!=null) {
            updateSelect();
        }
    }
    
    
    private void PopulateData() {
        if (hasDataInitialized()) {
            selectedId=getData().value();
            hasSelectedValue=true;
        }
        
        // initializes the list
        allMap=getListMap();
        // adds a leading empty value to not have a default selection
        allMap.put(Empty_Key, "");
        allMapKeys=allMap.keySet().toArray(new String[0]);
        
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        ChangeEvent eNew=new ChangeEvent(e.getSource());
        notifyAllChangeListeners(eNew);
    }

    @Override
    public boolean isEmpty() {
        return(fldList.getSelectedItem().equals(Empty_Key));
    }
    
    @Override
    public boolean isValid() {
        return(fldList.getSelectedIndex()!=-1);
    }

    private void updateSelect() {
        if (hasSelectedValue) {
            int foundIndex=-1;
            for (int i = 0; i < allMapKeys.length; i++) {
                String currId = allMapKeys[i];
                if (currId.equals(selectedId)) {
                    foundIndex=i;
                }
            }
            if (foundIndex>=0) {
                fldList.setSelectedIndex(foundIndex);
            }
        } 
    }

}
